package rest.o.gram.activities;

import android.app.Activity;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import rest.o.gram.R;
import rest.o.gram.RestogramPhoto;
import rest.o.gram.RestogramVenue;
import rest.o.gram.client.RestogramClient;
import rest.o.gram.common.Defs;
import rest.o.gram.location.ILocationTracker;
import rest.o.gram.location.Location;
import rest.o.gram.tasks.ITaskObserver;

/**
 * Created with IntelliJ IDEA.
 * User: Roi
 * Date: 16/04/13
 */
public class HomeActivity extends Activity implements ITaskObserver {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        tracker = RestogramClient.getInstance().getLocationTracker();
        location = tracker.getEstimation();
    }

    @Override
    public void onResume() {
        super.onResume();

        if (!foundMe)
        {
            findMe();
            foundMe = true;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        cancelProgress();
    }

    public void findMe() {
        this.location = tracker.estimate();

        final String provider = location.getLastOriginProvider();
        double radius;
        if (provider == null || provider  == LocationManager.NETWORK_PROVIDER)
            radius = Defs.Location.DEFAULT_NEARBY_RADIUS;
        else // if (provider  == LocationManager.GPS_PROVIDER)
            radius = Defs.Location.DEFAULT_FINDME_RADIUS;

        // Send get nearby request
        RestogramClient.getInstance().getNearby(location.getLastLat(),
                                                location.getLastLong(), radius, this);
    }

    @Override
    public void onFinished(RestogramVenue[] venues) {
        if(venues == null || venues.length == 0)
        {
            // Switch to "NearbyActivity" with parameters: "latitude", "longitude"
            Intent intent = new Intent(this, NearbyActivity.class);
            intent.putExtra("latitude", location.getLastLat());
            intent.putExtra("longitude", location.getLastLong());
            startActivityForResult(intent, Defs.RequestCodes.RC_NEARBY);
            return;
        }

        venue = venues[0]; // TODO: fix

        // Send get info request
        RestogramClient.getInstance().getInfo(venue.getId(), this);
    }

    @Override
    public void onFinished(RestogramVenue venue) {
        // Set image url to current venue member object
        this.venue.setImageUrl(venue.getImageUrl());

        // Switch to "VenueActivity" with parameter "venue"
        Intent intent = new Intent(this, VenueActivity.class);
        intent.putExtra("venue", this.venue);
        startActivityForResult(intent, Defs.RequestCodes.RC_VENUE);
    }

    @Override
    public void onFinished(RestogramPhoto[] photos) {
        // Empty
    }

    private void cancelProgress() {
        ProgressBar pb = (ProgressBar)findViewById(R.id.pbLoading);
        pb.setVisibility(View.GONE);
    }

    private ILocationTracker tracker;
    private Location location;
    private RestogramVenue venue;
    private boolean foundMe = false;
}
